<?php

class Obj_DBTable_Regions extends Zend_Db_Table_Abstract
{
    protected $_name = 'regions';
    protected $_primary = 'ID';

    
    public function getRegions($iCountry, $activeOnly = false) {
    	$select = $this->select()->from($this)->order('Name')->where('CountryID = ?', (int) $iCountry);
    	if ($activeOnly === true) $select->where('active = 1');
    	return $this->fetchAll($select)->toArray();
    }
    
    public function getAllRegions($activeOnly = false) {
    	$select = $this->select()->from($this)->order('Name');
    	if ($activeOnly === true) $select->where('active = 1');
    	return $this->fetchAll($select)->toArray();
    }
     
    public function getRegion($id) {
    	$select = $this->select()->from($this)->where('ID = ?', (int) $id);
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    }
     
    public function findRegion($alias) {
    	$select = $this->select()->from($this)->where('alias = ?', $alias)->where('active = 1');
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    } 
    
    public function getLinkRow($id) {
    	$select = $this->select()->from($this, array('a1' => 'alias'));
    	$select->join('countries', 'regions.CountryID = countries.ID', array('a0' => 'alias'));
    	$select->where('regions.ID = ?', $id);
    	$select->setIntegrityCheck(false);
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    }
}

